function e(e) {
    return function() {
        var t = e.apply(this, arguments);
        return new Promise(function(e, n) {
            function r(a, o) {
                try {
                    var s = t[a](o), u = s.value;
                } catch (e) {
                    return void n(e);
                }
                if (!s.done) return Promise.resolve(u).then(function(e) {
                    r("next", e);
                }, function(e) {
                    r("throw", e);
                });
                e(u);
            }
            return r("next");
        });
    };
}

var t = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
        var n = arguments[t];
        for (var r in n) Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[r]);
    }
    return e;
}, n = function(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}(require("../../utils/wxPromise.min.js")), r = require("../../utils/api.js"), a = require("../../utils/util.js"), o = null, s = wx.getRecorderManager(), u = null;

Page({
    data: {
        status: "free",
        count: 60,
        maxDuration: 600,
        form: {
            code: "",
            image: "",
            audio: "",
            top: 0
        },
        audio: {
            duration: "10s"
        },
        image: {
            width: 750,
            height: 1119.736842,
            scale: 1.31578
        },
        imageScale: 1,
        isIos: !1
    },
    chooseImage: function() {
        var a = this;
        return e(n.default.mark(function e() {
            var o, s, u, i, c, d, l, f, h, p, x, w, g;
            return n.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return e.next = 2, wx.pro.chooseImage({
                        count: 1
                    });

                  case 2:
                    if (o = e.sent, s = o.tempFilePaths, !((u = o.tempFiles)[0].size > 1048576)) {
                        e.next = 11;
                        break;
                    }
                    return e.next = 7, wx.pro.showModal({
                        content: "该图片大小超过1M，可能会造成打开速度变慢，建议取消勾选原图或压缩后再上传",
                        cancelText: "取消上传",
                        confirmText: "仍然上传"
                    });

                  case 7:
                    if (i = e.sent, c = i.confirm) {
                        e.next = 11;
                        break;
                    }
                    return e.abrupt("return");

                  case 11:
                    return d = s[0], e.next = 14, wx.pro.getImageInfo({
                        src: d
                    });

                  case 14:
                    return l = e.sent, f = l.width, h = l.height, p = f / h > 750 / 678 ? 678 / h : 750 / f, 
                    x = f * p, w = h * p, e.next = 22, (0, r.uploadFile)(d, {
                        loading: !1
                    });

                  case 22:
                    g = e.sent, a.setData({
                        form: t({}, a.data.form, {
                            image: g,
                            top: 0
                        }),
                        image: {
                            width: x,
                            height: w,
                            scale: Math.max(750 / f, 678 / h)
                        }
                    });

                  case 24:
                  case "end":
                    return e.stop();
                }
            }, e, a);
        }))();
    },
    imageTouchStart: function(e) {
        this.lastTouch = e.changedTouches[0];
    },
    getRpx: function(e) {
        var t = this.deviceInfo.screenWidth / 750 * e;
        return t > 0 ? Math.ceil(t) : Math.floor(t);
    },
    imageTouchMove: function(e) {
        var t = e.changedTouches[0], n = this.data.form.top + t.clientY - this.lastTouch.clientY;
        n = Math.min(0, n);
        var r = this.data.image.height;
        n = Math.max(this.getRpx(678 - r), n), this.lastTouch = t, this.setData({
            "form.top": n
        });
    },
    imageTouchEnd: function(e) {
        console.log("end", e);
    },
    stopRecord: function(e) {
        console.log("stop record"), s.stop(), this.setData({
            status: "recorded"
        });
    },
    authorize: function() {
        var t = this;
        return e(n.default.mark(function r() {
            var a, o;
            return n.default.wrap(function(r) {
                for (;;) switch (r.prev = r.next) {
                  case 0:
                    return r.next = 2, wx.pro.getSetting();

                  case 2:
                    if (a = r.sent, !1 !== (o = a.authSetting)["scope.record"]) {
                        r.next = 6;
                        break;
                    }
                    return r.abrupt("return", new Promise(function(r, a) {
                        wx.showModal({
                            title: "请打开录音功能",
                            content: "现在前往小程序设置开启",
                            success: function() {
                                var o = e(n.default.mark(function e(o) {
                                    var s, u;
                                    return n.default.wrap(function(e) {
                                        for (;;) switch (e.prev = e.next) {
                                          case 0:
                                            if (!o.cancel) {
                                                e.next = 3;
                                                break;
                                            }
                                            return a("user denied open setting"), e.abrupt("return");

                                          case 3:
                                            return e.next = 5, wx.pro.openSetting();

                                          case 5:
                                            if (s = e.sent, (u = s.authSetting)["scope.record"]) {
                                                e.next = 10;
                                                break;
                                            }
                                            return a("user denied auth record"), e.abrupt("return");

                                          case 10:
                                            return r(!0), e.abrupt("return");

                                          case 12:
                                          case "end":
                                            return e.stop();
                                        }
                                    }, e, t);
                                }));
                                return function(e) {
                                    return o.apply(this, arguments);
                                };
                            }(),
                            fail: a
                        });
                    }));

                  case 6:
                    return r.abrupt("return", wx.pro.authorize({
                        scope: "scope.record"
                    }));

                  case 7:
                  case "end":
                    return r.stop();
                }
            }, r, t);
        }))();
    },
    startRecord: function() {
        var t = this;
        return e(n.default.mark(function e() {
            return n.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return wx.setKeepScreenOn({
                        keepScreenOn: !0
                    }), e.next = 3, t.authorize();

                  case 3:
                    clearInterval(o), t.setData({
                        status: "recording",
                        count: t.data.maxDuration
                    }), s.start({
                        format: "mp3",
                        sampleRate: 22050,
                        encodeBitRate: 64e3,
                        duration: 1e3 * t.data.maxDuration
                    }), o = setInterval(function() {
                        t.data.count <= 0 ? clearInterval(o) : t.setData({
                            count: t.data.count - 1
                        });
                    }, 1e3);

                  case 7:
                  case "end":
                    return e.stop();
                }
            }, e, t);
        }))();
    },
    playAudio: function() {
        var e = this;
        wx.setKeepScreenOn({
            keepScreenOn: !0
        }), console.log("play record"), u && (u.destroy(), u = null), this.data.form.audio && (u = wx.createInnerAudioContext(), 
        wx.canIUse("setInnerAudioOption") && wx.setInnerAudioOption({
            obeyMuteSwitch: !1
        }), u.src = this.data.form.audio, u.play(), u.onEnded(function() {
            e.stopAudio();
        }), u.onPlay(function() {
            console.log("hello"), e.setData({
                status: "playing"
            });
        }), u.onError(function(t) {
            wx.showModal({
                showCancel: !1,
                content: t.errMsg
            }), e.stopAudio();
        }));
    },
    stopAudio: function() {
        console.log("stop audio"), u && u.destroy(), this.setData({
            status: "paused"
        });
    },
    updateQrcode: function() {
        var o = this;
        return e(n.default.mark(function e() {
            var s, u, i, c, d;
            return n.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    if (o.stopAudio(), (s = t({}, o.data.form)).image) {
                        e.next = 4;
                        break;
                    }
                    return e.abrupt("return", wx.showModal({
                        showCancel: !1,
                        content: "请上传图片"
                    }));

                  case 4:
                    return e.next = 6, wx.pro.showModal({
                        content: "发送后不支持修改，确定是否发送?"
                    });

                  case 6:
                    if (u = e.sent, i = u.confirm) {
                        e.next = 10;
                        break;
                    }
                    return e.abrupt("return");

                  case 10:
                    return wx.showLoading({
                        title: "上传音频中"
                    }), e.next = 13, (0, r.uploadFile)(s.audio);

                  case 13:
                    if (c = e.sent, wx.hideLoading(), s.audio = c, s.audio) {
                        e.next = 18;
                        break;
                    }
                    return e.abrupt("return", wx.showModal({
                        showCancel: !1,
                        content: "请上传录音"
                    }));

                  case 18:
                    return s.image = (0, a.getFileName)(s.image), s.audio = (0, a.getFileName)(s.audio), 
                    console.log(s), e.next = 23, (0, r.updateQrcode)(o.options.code, s);

                  case 23:
                    if (!(d = e.sent).errCode) {
                        e.next = 27;
                        break;
                    }
                    return wx.pro.showModal({
                        content: d.errMsg,
                        showCancel: !1
                    }), e.abrupt("return");

                  case 27:
                    wx.redirectTo({
                        url: "/pages/view/view?url=" + (0, a.getViewUrl)(o.options.code)
                    });

                  case 28:
                  case "end":
                    return e.stop();
                }
            }, e, o);
        }))();
    },
    bindevent: function() {
        var r = this;
        s.onStop(function() {
            var a = e(n.default.mark(function e(a) {
                var s;
                return n.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        clearInterval(o), s = a.tempFilePath, r.setData({
                            form: t({}, r.data.form, {
                                audio: s
                            }),
                            audio: {
                                duration: a.duration / 1e3
                            },
                            status: "recorded"
                        });

                      case 3:
                      case "end":
                        return e.stop();
                    }
                }, e, r);
            }));
            return function(e) {
                return a.apply(this, arguments);
            };
        }()), s.onError(function(e) {
            wx.showModal({
                showCancel: !1,
                content: e.errMsg
            }), r.stopRecord();
        });
    },
    onLoad: function() {
        var t = e(n.default.mark(function e(t) {
            return n.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    if (wx.hideShareMenu(), this.setData({
                        maxDuration: +(t.max_duration || 60)
                    }), this.deviceInfo = wx.getSystemInfoSync(), this.setData({
                        isIos: -1 !== this.deviceInfo.system.toLowerCase().indexOf("ios")
                    }), t.code) {
                        e.next = 9;
                        break;
                    }
                    return e.next = 7, wx.pro.showModal({
                        showCancel: !1,
                        content: "请扫描正确的二维码"
                    });

                  case 7:
                    return wx.reLaunch({
                        url: "/pages/index/index"
                    }), e.abrupt("return");

                  case 9:
                    this.bindevent();

                  case 10:
                  case "end":
                    return e.stop();
                }
            }, e, this);
        }));
        return function(e) {
            return t.apply(this, arguments);
        };
    }(),
    onHide: function() {
        console.log("hide page", this.data.status), "recording" === this.data.status && this.stopRecord();
    },
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});